package com.mp.common;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 *   全局异常处理器,程序抛出异常后自动被 "全局异常处理器" 进行处理，然后将对应信息反馈给用户
 *  @author:  notejava
 *  @date:  2020/8/14
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理自定义异常
     * @param e 参数
     * @return Result对象
     */
    @ExceptionHandler(value=BaseUncheckException.class)
    public Result handlerBaseUncheckException(BaseUncheckException e){
        return Result.exception(null,e.getCode(),e.getMsg());
    }

    /**
     * 路径不存在
     * @param e 参数
     * @return Result对象
     */
    @ExceptionHandler(value = NoHandlerFoundException.class)
    public Result handlerNoHandlerFoundException(NoHandlerFoundException e){
        return Result.exception(e,Code.EXCEPTION.getStatus(),Message.NO_FOUND.getMsg());
    }

    /**
     * 任何异常
     * @param e 参数
     * @return Result对象
     */
    @ExceptionHandler(value =Exception.class)
    public Result handlerException(Exception e){
        return Result.exception(e,Code.EXCEPTION.getStatus(),Message.SYSTEM_TIMEOUT.getMsg());
    }


}
